package com.example;

import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;

import org.apache.camel.main.Main;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * A Camel Application
 */
public class MainApp {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    public static void main(String... args) throws Exception {
        /*Main main = new Main();
        main.addRouteBuilder(new MyRouteBuilder());
        main.run(args);*/

        MainApp app = new MainApp();
        //app.test1();
        //app.test2();
        app.test3();
    }

    /**
     * 演示表格一
     */
    @Test
    public void test1() {
        logger.info("test1.....");
        // 创建word文档,并设置纸张的大小
        Document document = new Document(PageSize.A4);
        try {
            RtfWriter2.getInstance(document,
                    new FileOutputStream("outputDoc/演示表格1.doc"));

            document.open();

            //设置合同头
            Paragraph ph = new Paragraph();
            Font f = new Font();
            Paragraph p = new Paragraph("出口合同",
                    new Font(Font.NORMAL, 18, Font.BOLDITALIC, new Color(0, 0, 0)));
            p.setAlignment(1);
            try {
                document.add(p);
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            ph.setFont(f);

            // 设置中文字体
            // BaseFont bfFont =
            // BaseFont.createFont("STSongStd-Light",
            //"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // Font chinaFont = new Font();
            /*
             * 创建有三列的表格
             */
            //document.add(new Paragraph("生成表格"));
            //document.add(new Paragraph("用java生成word文件"));
            Table table = new Table(4);
            table.setBorderWidth(1);
            table.setBorderColor(Color.BLACK);
            //table.setPadding(0);
            //table.setSpacing(0);

            /*
             * 添加表头的元素
             */
            Cell cell = new Cell("表头");//单元格
            cell.setHeader(true);
            cell.setColspan(3);//设置表格为三列
            cell.setRowspan(3);//设置表格为三行
            table.addCell(cell);
            table.endHeaders();// 表头结束

            // 表格的主体
            cell = new Cell("Example cell 2");
            cell.setRowspan(2);//当前单元格占两行,纵向跨度
            table.addCell(cell);
            table.addCell("1,1");
            table.addCell("1,2");
            table.addCell("1,3");
            table.addCell("1,4");
            table.addCell("1,5");
            table.addCell(new Paragraph("用java生成的表格1"));
            table.addCell(new Paragraph("用java生成的表格2"));
            table.addCell(new Paragraph("用java生成的表格3"));
            table.addCell(new Paragraph("用java生成的表格4"));
            document.add(table);
            document.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 演示表格
     */
    @Test
    public void test2() {
        String file = "outputDoc/演示表格2.doc";
        try {
            createDocContext(file, "测试iText导出Word文档");
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 演示处理图片
     */
    private void test3(){
        try {
            createRTFContext("outputDoc/演示图片.doc");
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void createDocContext(String file, String contextString) throws DocumentException, IOException {
        //设置纸张大小
        Document document = new Document(PageSize.A4);
        //建立一个书写器，与document对象关联
        RtfWriter2.getInstance(document, new FileOutputStream(file));
        document.open();
        //设置中文字体
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        //标题字体风格
        Font titleFont = new Font(bfChinese, 12, Font.BOLD);
        //正文字体风格
        Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
        Paragraph title = new Paragraph("标题");
        //设置标题格式对齐方式
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(titleFont);
        document.add(title);
        Paragraph context = new Paragraph(contextString);
        context.setAlignment(Element.ALIGN_LEFT);
        context.setFont(contextFont);
        //段间距
        context.setSpacingBefore(3);
        //设置第一行空的列数
        context.setFirstLineIndent(20);
        document.add(context);
        //设置Table表格,创建一个三列的表格
        Table table = new Table(3);
        int width[] = {25, 25, 50};//设置每列宽度比例
        table.setWidths(width);
        table.setWidth(90);//占页面宽度比例
        table.setAlignment(Element.ALIGN_CENTER);//居中
        table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
        table.setAutoFillEmptyCells(true);//自动填满
        table.setBorderWidth(1);//边框宽度
        //设置表头
        Cell haderCell = new Cell("表格表头");
        haderCell.setHeader(true);
        haderCell.setColspan(3);
        table.addCell(haderCell);
        table.endHeaders();

        Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
        Cell cell = new Cell(new Paragraph("这是一个3*3测试表格数据", fontChinese));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);
        table.addCell(new Cell("#1"));
        table.addCell(new Cell("#2"));
        table.addCell(new Cell("#3"));

        document.add(table);
        document.close();
    }

    private void createRTFContext(String path) throws DocumentException,
            IOException {

        Document document = new Document(PageSize.A4);
        RtfWriter2.getInstance(document, new FileOutputStream(path));
        document.open();

        // 设置中文字体
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
                "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

        // 标题字体风格
        Font titleFont = new Font(bfChinese, 12, Font.BOLD);

        // 正文字体风格
        Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
        Paragraph title = new Paragraph("标题");

        // 设置标题格式对齐方式
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(titleFont);
        document.add(title);
        String contextString = "iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本，表格，图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。使用iText与PDF能够使你正确的控制Servlet的输出。";
        Paragraph context = new Paragraph(contextString);

        // 正文格式左对齐
        context.setAlignment(Element.ALIGN_LEFT);
        context.setFont(contextFont);

        // 离上一段落（标题）空的行数
        context.setSpacingBefore(20);

        // 设置第一行空的列数
        context.setFirstLineIndent(20);
        document.add(context);

        //在表格末尾添加图片
        Image png = Image.getInstance("media/1.png");
        png.setAlignment(Image.RIGHT);
        document.add(png);
        document.close();

    }


    /**
     * 字体统一使用等线体，一般的电脑上都会自带
     */
    @Test
    public void test4(){
        MyPrinter printer = new MyPrinter("outputDoc/002.doc");
        printer.print();
    }




}

